package com.stud.seckill.common.config;

import com.stud.seckill.common.enums.CacheKeyEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 缓存服务
 */
@Component
public class CacheService {

    @Autowired
    private RedisService redisService;

    /**
     * 设置缓存
     * @param cacheKeyEnum KEY
     * @param id
     * @param value 值
     */
    public void set(CacheKeyEnum cacheKeyEnum, Object id, Object value) {
        redisService.set(cacheKeyEnum.getKey() + id, value, cacheKeyEnum.getTimeout());
    }

    /**
     * 设置缓存
     * @param cacheKeyEnum KEY
     * @param id
     * @param value 值
     * @param timeout
     */
    public void set(CacheKeyEnum cacheKeyEnum, Object id, Object value, long timeout) {
        redisService.set(cacheKeyEnum.getKey() + id, value, timeout);
    }

    /**
     * 计数
     * @param cacheKeyEnum
     * @param id
     */
    public void increment(CacheKeyEnum cacheKeyEnum, Object id) {
        redisService.increment(cacheKeyEnum.getKey() + id);
    }

    /**
     * 查询
     * @param cacheKeyEnum
     * @param id
     * @return
     */
    public Object get(CacheKeyEnum cacheKeyEnum, Object id) {
        return redisService.get(cacheKeyEnum.getKey() + id);
    }

    /**
     * 是否存在KEY
     * @param cacheKeyEnum
     * @param id
     * @return
     */
    public Boolean hasKey(CacheKeyEnum cacheKeyEnum, Object id) {
        return redisService.hasKey(cacheKeyEnum.getKey() + id);
    }

    /**
     * 删除
     * @param cacheKeyEnum
     * @param id
     */
    public void delete(CacheKeyEnum cacheKeyEnum, Object id) {
        redisService.delete(cacheKeyEnum.getKey() + id);
    }

}
